package top.miay.snowid;

import java.net.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Enumeration;

/**
 * @description: snowid-generator工具类
 * @author 猿猿长成记
 */
public class SnowIdUtil {
    public static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static String getIp(IpType ipType, String eth) throws Exception {
        String ip = null;
        if (eth == null) {
            if(ipType==IpType.IPv4){
                ip = getIpv4();
            }else{
                ip = getIpv6();
            }
        }

        if(ip!=null){
            return ip;
        }

        if(eth==null){
            throw new SnowIdException("Failed to obtain a valid IP address. You can specify the network interface using the \"%s\" parameter.", new Object[]{"appInfo.eth"});
        }else{
            ip = getNetInterfaceIp(eth, ipType);
            if(ip==null){
                throw new SnowIdException("No valid IP address could be obtained from the specified network  \"%s\". Please check the application configuration.", new Object[]{eth});
            }
        }
        return ip;
    }

    public static String getIpv4() throws UnknownHostException {
        InetAddress localHost = Inet4Address.getLocalHost();
        if(localHost==null || localHost.isLoopbackAddress()){
            return null;
        }
        return localHost.getHostAddress();
    }

    public static String getIpv6() throws UnknownHostException {
        InetAddress localHost = Inet6Address.getLocalHost();
        if(localHost==null || localHost.isLoopbackAddress()){
            return null;
        }
        return localHost.getHostAddress();
    }

    /**
     * 根据指定网卡名称获取IP
     * @param name 网卡名称;
     * @param ipType ip类型;
     * @return ip;
     * */
    public static String getNetInterfaceIp(String name, IpType ipType) throws SocketException {
        Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
        while (nets.hasMoreElements()) {
            NetworkInterface net = nets.nextElement();
            if(net.getDisplayName().equals(name)) {
                Enumeration<InetAddress> addrs = net.getInetAddresses();
                while (addrs.hasMoreElements()) {
                    InetAddress addr = addrs.nextElement();
                    if (!addr.isLoopbackAddress()) {
                        if(addr instanceof Inet4Address && ipType==IpType.IPv4) {
                            return addr.getHostAddress();
                        }else if(addr instanceof Inet6Address && ipType==IpType.IPv6){
                            return addr.getHostAddress();
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * 获取当前时间秒数
     */
    public static long getCurrentTimeSecond() {
        return LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * 根据时间字符串获取时间秒数
     * */
    public static long getTimeSecond(String timeStr){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_TIME_FORMAT);
        return LocalDateTime.parse(timeStr, formatter).atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * 根据时间秒数还原时间信息
     * */
    public static LocalDateTime getLocalDateTime(long timeSecond){
        return LocalDateTime.ofInstant(Instant.ofEpochSecond(timeSecond), ZoneId.systemDefault());
    }

}
